package com.courseware.online.common;

import com.courseware.online.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.thymeleaf.expression.Lists;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * @author : demos
 */
@Slf4j
@Component
public class CommonUtil {

    @Autowired
    private AppConfig appConfig;
    public String getFilePath(String name) {
        String dir = appConfig.getDowloadPath();
        createDir(dir);
        return dir + name + ".pptx";
    }

    // 创建目录
    public void createDir(String destDirName) {
        File dir = new File(destDirName);
        if (dir.exists()) {
            return;
        }
        if (!destDirName.endsWith(File.separator)) {
            destDirName = destDirName + File.separator;
        }
        //创建目录
        if (dir.mkdirs()) {
            log.info("创建目录" + destDirName + "成功！");
        } else {
            log.error("创建目录" + destDirName + "失败！");
        }
    }

    public static <T, S> List<T> mapList(final Mapper mapper, List<S> sourceList, Class<T> targetObjectClass) {
        List<T> targetList = new ArrayList<T>();
        for (S s : sourceList) {
            targetList.add(mapper.map(s, targetObjectClass));
        }
        return targetList;
    }
}
